import org.json.JSONException;
import org.json.JSONObject;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

/**
 * Created by qupen on 2016/8/17.
 */
public class SendMessage extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        String host = request.getParameter("host");
        String port = request.getParameter("port");
        String action = request.getParameter("action");
        String method = request.getParameter("method");
        String requester = request.getParameter("requester");
        String dataString = request.getParameter("data");
        String httpsUrl = "https://" + host + ":" + port + "/" + action;

        try {
            JSONObject object = new JSONObject(dataString);
            object.put("time", new Date().getTime());

            if (requester.equalsIgnoreCase("message")) {
                dataString = "";
                /*
                Iterator keys = object.keys();
                while (keys.hasNext()) {
                    dataString += object.getString(keys.next().toString()) + "|";
                }
                */
                dataString += object.getString("version") + "|";
                dataString += object.getString("time") + "|";
                dataString += object.getString("gatewayId") + "|";
                dataString += object.getString("machineId") + "|";
                dataString += object.getString("port") + "|";
                dataString += object.getString("sensorType") + "|";
                dataString += object.getString("rawData") + "|";

                dataString = dataString.substring(0, dataString.length() - 1);
            } else {
                dataString = object.toString();
            }
        } catch (JSONException jse) {
            jse.printStackTrace();
        }

        PrintWriter out = response.getWriter();

        String currentPath = getServletContext().getRealPath("/");
        // 密码
        String password = "localhost";
        // 密钥库
        String keyStorePath = currentPath + "\\tomcat.keystore";
        // 信任库
        String trustStorePath = currentPath + "\\tomcat.keystore";
        // 传输文本
        try {
            HttpsHelper.initHttpsURLConnection(password, keyStorePath, trustStorePath);
            // 发起请求
            String postResult = HttpsHelper.post(httpsUrl, dataString);
            out.println(postResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        ServletOutputStream out = response.getOutputStream();
        String data = "{'version': '1', 'id': 'A012345', 'time': '1471247100320', 'ports':[{'number':'0', 'status':'1'}, {'number':'1', 'status':'0'}], 'power':'1'}";
        out.write(data.getBytes());
    }
}
